import { Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Model } from "mongoose";
import { SystemDocument } from "./schemas/system.schemas";

@Injectable()
export class SystemService {
  constructor(@InjectModel("System") private systemModel: Model<SystemDocument>) {}

  // 创建菜单
  async createMenu(params) {
    // 直接假数据
    const mockList = [
      {
        name: "大屏展示",
        index: "1",
        children: [
          { name: "一号大屏", index: "1-1" },
          { name: "二号大屏", index: "1-2" },
        ],
      },
      {
        name: "二号菜单",
        index: "2",
      },
      {
        name: "三号菜单",
        index: "3",
        children: [
          { name: "3-1", index: "3-1" },
          { name: "3-2", index: "3-2" },
        ],
      },
    ];
    await this.systemModel.create(mockList);
    return {};
  }

  // 获取菜单
  async getMenu() {
    const results = await this.systemModel.find().sort("index");
    return results.map(item => {
      return {
        id: item.id,
        name: item.name,
        index: item.index,
        children: item.children,
      };
    });
  }
}
